package com.example.demo.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.demo.dao.BookMapper;
import com.example.demo.dao.BookRepository;
import com.example.demo.model.Book;
import com.example.demo.model.BookBySearch;
import com.example.demo.model.Role;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHits;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;
import com.example.demo.dao.RoleMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/12/18 8:58
 */
@Service
public class SearchService {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private ElasticsearchRestTemplate elasticsearchRestTemplate;
    @Autowired
private BookRepository bookRepository;
    public void ToEs()
    {
        ArrayList<BookBySearch> bookBySearches = new ArrayList<>();
        List<com.example.demo.model.Book> booksbyname = bookMapper.selectList(new QueryWrapper<com.example.demo.model.Book>().select("id","name","type","author"));

        for (Book book : booksbyname) {
            bookRepository.save(book);
          }
    }
    public  void search(String keyWord) {
        ArrayList<BookBySearch> bookBySearches = new ArrayList<>();
        List<com.example.demo.model.Book> booksbyname = bookMapper.selectList(new QueryWrapper<com.example.demo.model.Book>().select("id","name","type","author"));

      //  List<Role> roles = roleMapper.selectList(new QueryWrapper<Role>().select("bookid", "rolename"));
//        for (com.example.demo.model.Book book : booksbyname) {
//            BookBySearch bookBySearch = new BookBySearch(book.getId(), book.getName(), book.getType(), book.getAuthor());
//
//             bookBySearches.add(bookBySearch);
//        }
        MultiMatchQueryBuilder multiMatchQueryBuilder = QueryBuilders.multiMatchQuery(keyWord, "name", "author");
//        FuzzyQueryBuilder fuzzyname = QueryBuilders.fuzzyQuery("name", keyWord);
//        FuzzyQueryBuilder fuzzyauthor = QueryBuilders.fuzzyQuery("author", keyWord);
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
        NativeSearchQuery build = nativeSearchQueryBuilder./*withFilter(fuzzyauthor).withFilter(fuzzyname)*/withQuery(multiMatchQueryBuilder).build();
      //  SearchHits<Book> books = elasticsearchRestTemplate.search(build, Book.class);
        List<Book> books1 = elasticsearchRestTemplate.multiGet(build, Book.class);
        for (Book book : books1) {
            System.out.println(book);
        }

    }
}
